package com.hsxc.cdz.controller.consoleController;

/** 
* @author 作者 : 陈钊
* @version 创建时间：2016年8月25日 上午10:09:22 
* 类说明：  
*/
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsxc.cdz.base.bean.Bill;
import com.hsxc.cdz.base.util.TimeFormatter;
import com.hsxc.cdz.mgmt.GlobalManager;
import com.hsxc.cdz.trans.bill.CacheTrade;
import com.hsxc.cdz.trans.payment.gzhpay.WXUtil;

@Controller
@RequestMapping("/api")
public class PaymentCallback {
	Logger logger = LoggerFactory.getLogger(PaymentCallback.class);
	@RequestMapping(value = "/paymentCallback", method = RequestMethod.POST)
	@ResponseBody
	public Object paymentCallback(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
			HttpSession httpSession) {
		SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
		try {
			InputStream inStream = httpServletRequest.getInputStream();
			ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = inStream.read(buffer)) != -1) {
				outSteam.write(buffer, 0, len);
			}
			logger.info("--------------START TO NOTIFY PAYMENT RESULT--------------");
			outSteam.close();
			inStream.close();
			String result = new String(outSteam.toByteArray(), "utf-8");//
			Map<String, String> map = WXUtil.parseXml(result);

			for (String key : map.keySet()) {
				logger.info(key + ":" + map.get(key));
			}
			if (map.get("result_code").toString().equalsIgnoreCase("SUCCESS")) {
				parameters.put("return_code", "SUCCESS");
				parameters.put("return_msg", "OK");
				logger.info("------PAYMENT IS SUCCESSFULE-------");
				String openId = map.get("openid");
				logger.info("openid" + openId);
				CacheTrade cacheTrade = GlobalManager.tradeManager.getTradeByOpenId(openId);
				logger.info("支付成功后的cachetrade" + cacheTrade);
				if (cacheTrade == null) {
					logger.info("这里不可能执行");
				} else {
					Bill bill = GlobalManager.billManager.getBillByBillNO(cacheTrade.getTid());
					if (bill.getState() != Bill.PAYED) {
						bill.setState(Bill.PAYED);
						bill.setPayWay(Bill.WXGZH);
						bill.setPayTime(TimeFormatter.toTimeYear());
						int i = GlobalManager.billManager.updateBill(bill);
						logger.info("UPDATE BILL STATUS TO PAYED RETURN " + i);
						GlobalManager.tradeManager.delTradeByOpenId(openId);
					}
				}
			} else {
				parameters.put("return_code", map.get("return_code"));
				parameters.put("return_msg", map.get("return_msg"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.info("PAYMENT NOTIFY END");
		return parameters;
	}
}
